package gms;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Date;

/**
 * This is a class to help manage information about
 * boxoffices wherever they might be. 
 * 
 * This should be used by the GMS and whatever
 * else needs a view (and deems this implementation
 * applicable) 
 * 
 * @author psyomn
 *
 */

public class View
{
	/**
	 * This is the hostname where the boxoffice is located
	 */
	private String mHostname; 
	
	/**
	 * The name of the box office we assigned
	 */
	private String mBoxname; 
	
	/**
	 * The last ping time from the boxoffice
	 */
	private Date mLastPingTime; 
	
	/** Default constructor */
	public View(String hostname, String boxname)
	{
		mHostname = hostname; 
		mBoxname = boxname; 
		mLastPingTime = new Date(); 
	}
	
	/**
	 * This just updates the timestamp of the view
	 */
	public void update()
	{
		mLastPingTime = new Date(); 
	}
	
	/**
	 * This is to retrieve the difference in seconds of
	 * the last time of ping. You will notice divisions
	 * of thousands because java uses milliseconds for
	 * timestamps instead of seconds as the typical unix
	 * timestamp. 
	 * @return the difference in seconds from last ping
	 */
	public long getDifference()
	{
		return 
				((new Date()).getTime() / 1000) // unix timestamp now 
				- (mLastPingTime.getTime() / 1000); // unix timestamp then 
	}
	
	/**
	 * This returns the last ping time of this view
	 * @return the last ping time as Date obj
	 */
	public Date getLastPingTime()
	{
		return mLastPingTime; 
	}
	
	/**
	 * This returns the hostname we set the view in
	 * @return the hostname of the view
	 */
	public String getHostname()
	{
		return mHostname; 
	}
	
	/**
	 * For debug info
	 */
	public String toString()
	{
		return
				mBoxname + ":" 
				+ mHostname + ":" 
				+ mLastPingTime; 
	}
	
	public InetAddress getIP()
	{
		try
    {
	    return InetAddress.getByName(mHostname);
    } 
		catch (UnknownHostException e)
    {
	    e.printStackTrace();
	    return null;
    }
	}
	
	public String getBoxName()
	{
		return mBoxname;
	}
}
